//define(function(require,exports) {
'use strict';

angular
    .module('shopApp', [
        'ngRoute',
        'ngTouch',
        "ngSanitize",
    ])
    .config(["$routeProvider","$httpProvider",function ($routeProvider,$httpProvider) {
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers
            .common['X-Requested-With'];
        $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
        $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
        // Override $http service's default transformRequest
        $httpProvider.defaults.transformRequest = [function(data)
        {
            /**
             * The workhorse; converts an object to x-www-form-urlencoded serialization.
             * @param {Object} obj
             * @return {String}
             */
            var param = function(obj)
            {
                var query = '';
                var name, value, fullSubName, subName, subValue, innerObj, i;

                for(name in obj)
                {
                    value = obj[name];

                    if(value instanceof Array)
                    {
                        for(i=0; i<value.length; ++i)
                        {
                            subValue = value[i];
                            fullSubName = name + '[' + i + ']';
                            innerObj = {};
                            innerObj[fullSubName] = subValue;
                            query += param(innerObj) + '&';
                        }
                    }
                    else if(value instanceof Object)
                    {


                        for(subName in value)
                        {


                            subValue = value[subName];
                            if(subValue != null){
                                // fullSubName = name + '[' + subName + ']';
                                fullSubName = name + '.' + subName;
                                // fullSubName =  subName;
                                innerObj = {};
                                innerObj[fullSubName] = subValue;
                                query += param(innerObj) + '&';
                            }
                        }
                    }
                    else if(value !== undefined ) //&& value !== null
                    {
                        query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
                    }
                }


                return query.length ? query.substr(0, query.length - 1) : query;
            };


            return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
        }];
        $routeProvider
            .when('/', {
                // templateUrl: 'shop/category.html',
                template:ui.category,
                controller: 'CateCtrl'
            })
            .when('/about', {
                templateUrl: 'shop/about.html',
                controller: 'AboutCtrl'
            })
            .when('/list', {
                templateUrl: 'shop/list.html',
                controller: 'ListCtrl'
            })
            .when('/success', {
                templateUrl: 'shop/success.html',
                controller: 'SuccessCtrl'
            })
            .when('/pay', {
                templateUrl: 'shop/pay.html',
                controller: 'PayCtrl'
            })
            .otherwise({
                redirectTo: '/'
            });
    }])
    .directive("scrolly",function($window){
        //分页
        return{
            restrict: 'A',
            link: function(scope, element, attrs) {
                angular.element($window).bind("scroll", function (e) {
                    if (($window.scrollY - $window.scrollMaxY > 0)) {
                        scope.pages =false;
                        //console.log(attrs);
                        scope.$apply(attrs.scrolly);
                    }
                })
            }
        }
    });
//});


var ui = {
    category: '<div class="m-choice g-container">'+
    '    <div class="hd">'+
    '        <a class="g-xs4" href="javascript:;" ng-class="{\'z-sel\':tab==1}" ng-click="tab=tab==1 ? 0:1">'+
    '            {{area?area:"区域"}}'+
    '            <i class="icon-font" title="向下">&#xe617;</i>'+
    '        </a>'+
    '        <a class="g-xs4" href="javascript:;" ng-model="type" ng-class="{\'z-sel\':tab==2}" ng-click="tab=tab==2 ? 0:2">'+
    '            {{types?types:"类型"}}'+
    '            <i class="icon-font" title="向下">&#xe617;</i>'+
    '        </a>'+
    '        <a class="g-xs4" href="javascript:;" ng-class="{\'z-sel\':tab==3}" ng-click="tab=tab==3 ? 0:3">'+
    '            {{sorts?sorts:"排序"}}'+
    '            <i class="icon-font" title="向下">&#xe617;</i>'+
    '        </a>'+
    '    </div>'+
    '    <div class="list" ng-show="tab==1">'+
    '        <ul>'+
    '            <li ng-click="areaCode=\'\';area=\'全城\';tab=0;"><a href="javascript:;">全城</a> <i class="icon-font" title="选择" ng-show="areaCode==\'xxx\'">&#xe618;</i></li>'+
    '        </ul>'+
    '    </div>'+
    '    <div class="list" ng-show="tab==2">'+
    '        <ul>'+
    '           <li ng-click="typeIds=\'\';types=\'全部分类\';tab=0;">'+
    '            <a href="#/?areaCode={{areaCode}}&area={{area}}&typeIds={{typeIds}}&types={{types}}&shopSorter={{shopSorter}}">全部分类</a>'+
    '             <i class="icon-font" title="选择" ng-show="typesId=\'\'">&#xe618;</i>'+
    '           </li>'+
    '           <li ng-repeat="tpList in typesList">'+
    '           <a href="#/?areaCode={{areaCode}}&area={{area}}&typeIds={{tpList.id}}&types={{tpList.name}}&shopSorter={{shopSorter}}" ng-class="{\'z-sel\':typesId==tpList.id}">{{tpList.name}}</a>'+
    '            <i class="icon-font" title="选择" ng-show="typesId==\'{{tpList.id}}\'">&#xe618;</i>'+
    '           </li>'+
    '        </ul>'+
    '    </div>'+
    '    <div class="list" ng-show="tab==3">'+
    '        <ul>'+
    '            <li ng-repeat="sortItem in sortList">'+
    '            <a href="#/?areaCode={{areaCode}}&area={{area}}&typeIds={{typeIds}}&types={{types}}&shopSorter={{sortItem.id}}" ng-class="{\'z-sel\':shopSorter==sortItem.id}">'+
    '                {{sortItem.zh}}'+
    '             </a>'+
    '                <i class="icon-font" title="选择" ng-show="shopSorter==sortItem.id">&#xe618;</i>'+
    '            </li>'+
    '        </ul>'+
    '    </div>'+
    '</div>'+
    '<!--<div class="g-container"><p style="line-height: 3rem;" ng-hide="hasShop">未找到合适的的数据!</p></div>-->'+
    '<div class="g-container m-shop" ng-class="showCate ? \'z-sel\' : \'\'">'+
    '    <ul>'+
    '        <!--ng-repeat="shop in shophList"-->'+
    '        <li>'+
    '            <a href="#/about/{{shop.id}}">'+
    '                <div class="cover"><img src="http://placehold.it/162x135/7ecdc5/fff" ng-src="{{shop.shopLogoUrl}}" alt=""/></div>'+
    '                <div class="g-rows tt">'+
    '                    <p class="g-xs6">'+
    '                        <span class="name">{{shop.shopName}}</span>'+
    '                        <span class="location">{{shop.shoppingMall}}   {{shop.typeNames}}</span>'+
    '                    </p>'+
    '                    <p class="g-xs6 f-tar">'+
    '                        <span class="price">{{shop.perConsume}}</span>'+
    '                        <span class="distance"> <i class="icon-font" title="定位">&#xe619;</i> {{shop.location}}KM</span>'+
    '                    </p>'+
    '                </div>'+
    '            </a>'+
    '        </li>'+
    '        <li>'+
    '            <a href="#/about/{{shop.id}}">'+
    '                <div class="cover"><img src="http://placehold.it/162x135/7ecdc5/fff" ng-src="{{shop.shopLogoUrl}}" alt=""/></div>'+
    '                <div class="g-rows tt">'+
    '                    <p class="g-xs6">'+
    '                        <span class="name">{{shop.shopName}}</span>'+
    '                        <span class="location">{{shop.shoppingMall}}   {{shop.typeNames}}</span>'+
    '                    </p>'+
    '                    <p class="g-xs6 f-tar">'+
    '                        <span class="price">{{shop.perConsume}}</span>'+
    '                        <span class="distance"> <i class="icon-font" title="定位">&#xe619;</i> {{shop.location}}KM</span>'+
    '                    </p>'+
    '                </div>'+
    '            </a>'+
    '        </li>'+
    '        <li>'+
    '            <a href="#/about/{{shop.id}}">'+
    '                <div class="cover"><img src="http://placehold.it/162x135/7ecdc5/fff" ng-src="{{shop.shopLogoUrl}}" alt=""/></div>'+
    '                <div class="g-rows tt">'+
    '                    <p class="g-xs6">'+
    '                        <span class="name">{{shop.shopName}}</span>'+
    '                        <span class="location">{{shop.shoppingMall}}   {{shop.typeNames}}</span>'+
    '                    </p>'+
    '                    <p class="g-xs6 f-tar">'+
    '                        <span class="price">{{shop.perConsume}}</span>'+
    '                        <span class="distance"> <i class="icon-font" title="定位">&#xe619;</i> {{shop.location}}KM</span>'+
    '                    </p>'+
    '                </div>'+
    '            </a>'+
    '        </li>'+
    '    </ul>'+
    '</div>'

}